<?php
class AdAction extends FrontAction {
    private $db = FALSE;
    protected function init() {
        if(!$this->checkConsoleLogin()) {
            $this->redirect('member/login');
            exit;
        }
        $this->assign('user',$this->user);
        $this->assign('user_info',$this->user_info);
        $this->assign('root_path',$this->root_path);
        $this->assign('ad_position',W('adPosition',array(),true));
    }
    public function index() {
        $data = $this->getPageData('ad',20);
        $this->assign('data',$data);
        $this->display();
    }
    public function adAdd() {
        $this->display();
    }
    public function adAddPost() {
        if($this->post) {
            if (!$this->validTR(true)){
                alert('参数非法','','2');
                exit;
            }
            if(!$_POST['ad_position'] || !$_POST['ad_type']) {
                alert('请填写所有的选项','','2');
                exit;
            }
            import("ORG.Net.UploadFile");
            $upload = new UploadFile(5097152,'jpg,jpeg,gif,png,swf',array(),'./u/','uniqid');
            $upload->autoSub = true;
            $upload->subType = 'date';
            $upload_info = $upload->uploadOne('ad');
            if($upload->getErrorMsg() !== ''){
                alert($upload->getErrorMsg(),'','2');
                exit;
            }
            if($upload_info){
                $data['ad_position'] = abs($_POST['ad_position']);
                $data['ad_type'] = abs($_POST['ad_type']);
                $data['img_path'] = $upload_info[0]['savename'];
                $data['ad_title'] = h($_POST['ad_title']);
                $data['ad_link'] = (strpos($_POST['ad_link'],'http://')===0 || $_POST['ad_link'] == '') ? $_POST['ad_link'] : 'http://'.$_POST['ad_link'];
                M('ad')->data($data)->add();
                $slider = M('ad')->where(array('ad_position'=>1))->limit('0,5')->order('aid DESC')->findAll();
                foreach((array)$slider as $v){
                    $tmp[]=array('title'=>$v['ad_title'],'src'=>'/u/'.$v['img_path'],'url'=>$v['ad_link'],'cont' => '');
                }
                $slider = json_encode($tmp);
                F('slider',$slider,DATA_PATH.'ad/');
                alert('添加成功','ad/index');
            }
            exit;
        }
    }
}